//
//  CollectionViewTwo.m
//  Feel_at_home
//
//  Created by dlios on 15/9/29.
//  Copyright © 2015年 Feel_at_home. All rights reserved.
//

#import "CollectionViewTwo.h"
#import "WidthAndHeight.pch"
#import "HTTPTOOL.h"
#import "CollectionViewCell.h"


@implementation CollectionViewTwo

// 初始化
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.itemSize = CGSizeMake(70, 130);
        flowLayout.minimumInteritemSpacing = (self.frame.size.width - 70 * 4) / 5;
        
        flowLayout.minimumLineSpacing = 0;
        flowLayout.sectionInset = UIEdgeInsetsMake(0, (VIEWWIDTH - 70 * 4) / 5, 0, (VIEWWIDTH - 70 * 4) / 5);
        
        self.collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, VIEWWIDTH, 130) collectionViewLayout:flowLayout];
        self.collection.backgroundColor = [UIColor clearColor];
        self.collection.delegate = self;
        self.collection.dataSource = self;
        [self addSubview:self.collection];
        
        [self.collection registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    }
    [self DataHeadel];
    return self;
}

// 网络解析
-(void)DataHeadel
{
    
    NSString * str = @ClassCollectionUrl;
    
    [HTTPTOOL GETWithURL:str body:nil httpHead:nil responseStyle:JSON success:^(id result) {
        
        self.nameArr = [NSMutableArray array];
        self.picArr = [NSMutableArray array];
        self.idArr = [[NSMutableArray alloc]init];
        NSArray * arr =[[[[result objectForKey:@"data"] objectForKey:@"channel_groups"] objectAtIndex:1]objectForKey:@"channels"];
        for (int i = 0; i < arr.count; i++) {
            NSString *name = [[arr objectAtIndex:i]objectForKey:@"name"];
            NSString *pic = [[arr objectAtIndex:i]objectForKey:@"icon_url"];
            NSString *idStr = [[arr objectAtIndex:i]objectForKey:@"id"];
            [self.nameArr addObject:name];
            [self.picArr addObject:pic];
            [self.idArr addObject:idStr];
            [self.collection reloadData];
        }
        
    } fail:^(NSError *error) {
        
        NSLog(@"%@", error);
    }];
    
}

// row
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

// section
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.nameArr.count;
}

// cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    cell.lable.text = [self.nameArr objectAtIndex:indexPath.item];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[self.picArr objectAtIndex:indexPath.item]]];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        cell.imageview.image = [UIImage imageWithData:data];
    }];
    
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *str = [NSString stringWithFormat:@ClassCollectionTwoUrl, self.idArr[indexPath.item]];
    [self.delegate ThiredTwoDelegate:str];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
